package com.squirrel.michale;

import java.util.Stack;


/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/12 9:51 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0880_2 {


    public String decodeAtIndex6(String s, int k) {
        long size = 0;
        int n = s.length();
        for (int i = 0; i < n; ++i) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {
                size *= c - '0';
            } else {
                size++;
            }
        }

        for (int i = n - 1; i >= 0; --i) {
            char c = s.charAt(i);
            k %= size;
            if (k == 0 && Character.isLetter(c)) {
                return Character.toString(c);
            }
            if (Character.isDigit(c)) {
                size /= c - '0';
            } else {
                size--;
            }
        }
        return "";
    }


    public String decodeAtIndex(String s, int k) {

        Stack<Part> partStack = new Stack<>();
        char[] chars = s.toCharArray();

        Part currentPart = new Part("", 0, 0);
        ;
        StringBuffer suffix = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (Character.isDigit(aChar)) {
                int repeat = aChar - '0';
                int partLength = currentPart.partLength * currentPart.repeat + suffix.length();
                Part part = new Part(suffix.toString(), partLength, repeat);
                partStack.push(part);
                currentPart = part;
                suffix.setLength(0);
            } else {
                suffix.append(aChar);
            }
        }
        if (suffix.length() > 0) {
            int repeat = 1;
            int partLength = currentPart.partLength * currentPart.repeat + suffix.length();
            Part part = new Part(suffix.toString(), partLength, repeat);
            partStack.push(part);
        }


        while (!partStack.isEmpty()) {
            Part part = partStack.pop();
            if (part.repeat > 0) {
                if (k > part.partLength) {
//                    int index = k - 1;
                    k = k % part.partLength;
                    if (part.suffix.equals("")) {
                        continue;
                    } else {
                        if (k <= part.partLength && k > part.partLength - part.suffix.length()) {
                            // k在当前Part的suffix中
                            return String.valueOf(part.suffix.charAt(k + part.suffix.length() - part.partLength - 1));
                        } else if (k <= part.partLength - part.suffix.length()) {
                            // k 在下一个Part的suffix中
                            continue;
                        }
                    }

                } else if (k <= part.partLength && k > part.partLength - part.suffix.length()) {
                    // k在当前Part的suffix中
                    return String.valueOf(part.suffix.charAt(k + part.suffix.length() - part.partLength - 1));
                } else if (k <= part.partLength - part.suffix.length()) {
                    // k 在下一个Part的suffix中
                    continue;
                }
            }
        }


        return "";
    }


    class Part {

        String suffix;

        int partLength;
        int repeat;


        public Part(String suffix, int partLength, int repeat) {
            this.suffix = suffix;
            this.partLength = partLength;
            this.repeat = repeat;
        }
    }


    public static void main(String[] args) {
        String s1 = "leet2code3";
        String s2 = "ha22";
        String s3 = "a2345678999999999999999";

        LeetCode0880_2 leetCode0880 = new LeetCode0880_2();

        System.out.println(leetCode0880.decodeAtIndex(s1, 10));
        System.out.println("=======================");
        System.out.println(leetCode0880.decodeAtIndex(s2, 5));
        System.out.println("=======================");
//        leetCode0880.treatString(s3);
        System.out.println(leetCode0880.decodeAtIndex(s3, 1));
        System.out.println("=======================");

        String s4 = "abc";
        int k4 = 1;
//        添加到测试用例
//                输出
//        ""
//        预期结果
//        "a"
        System.out.println(leetCode0880.decodeAtIndex(s4, k4));

        System.out.println("=======================");

        String s5 = "a23";
        int k5 = 6;

//                输出
//        ""
//        预期结果
//        "a"
        System.out.println(leetCode0880.decodeAtIndex2(s5, k5));

        System.out.println("=======================");

        String s6 = "a2b3c4d5e6f7g8h9";
        int k6 = 9;

        leetCode0880.decodeAtIndex2(s6, k6);


    }

    public String decodeAtIndex2(String s, int k) {

        Stack<Part> partStack = new Stack<>();
        char[] chars = s.toCharArray();

        Part currentPart = new Part("", 0, 0);
        ;
        StringBuffer suffix = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (aChar >= 49 && aChar <= 57) {
                int repeat = Integer.parseInt(String.valueOf(aChar));
                int partLength = currentPart.partLength * currentPart.repeat + suffix.length();
                Part part = new Part(suffix.toString(), partLength, repeat);
                partStack.push(part);
                currentPart = part;
                suffix.setLength(0);
            } else {
                suffix.append(aChar);
            }
        }
        if (suffix.length() > 0) {
            int repeat = 1;
            int partLength = currentPart.partLength * currentPart.repeat + suffix.length();
            Part part = new Part(suffix.toString(), partLength, repeat);
            partStack.push(part);
        }


        while (!partStack.isEmpty()) {
            Part part = partStack.pop();
            if (part.repeat > 0) {
                if (k > part.partLength) {
//                    int index = k - 1;
                    k = k % part.partLength;
                    if (part.suffix.equals("")) {
                        continue;
                    } else {
                        if (k <= part.partLength && k > part.partLength - part.suffix.length()) {
                            // k在当前Part的suffix中
                            return String.valueOf(part.suffix.charAt(k + part.suffix.length() - part.partLength - 1));
                        } else if (k <= part.partLength - part.suffix.length()) {
                            // k 在下一个Part的suffix中
                            continue;
                        }
                    }

                } else if (k <= part.partLength && k > part.partLength - part.suffix.length()) {
                    // k在当前Part的suffix中
                    return String.valueOf(part.suffix.charAt(k + part.suffix.length() - part.partLength - 1));
                } else if (k <= part.partLength - part.suffix.length()) {
                    // k 在下一个Part的suffix中
                    if (partStack.isEmpty()) {
                        return part.suffix.substring(part.suffix.length() - 1);
                    }
                    continue;
                }
            }
        }


        return "";
    }
}
